package com.test.restruant.controller;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.test.restruant.util.FastDFSClient;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;

@Controller
public class UploadController {
    @RequestMapping(value = "/upload.html")
    public String fileUpload1(@RequestParam("file") MultipartFile file) {
        String string = null;
        try {
            FastDFSClient fastDFSClient = new FastDFSClient(
                    "classpath:client.conf");
            String name = file.getOriginalFilename();
            String extName = name.substring(name.lastIndexOf(".") + 1);
            string = fastDFSClient.uploadFile(file.getBytes(), extName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(string);

        return "upload";
    }
//    @RequestMapping(value = "/upload.html")
//    public String fileUpload1(@RequestParam("file") MultipartFile file) {
//        try {
////            String name = file.getOriginalFilename();
////            String extName = name.substring(name.lastIndexOf("."));
////            //将上传的文件存在E:/upload/下
////            FileUtils.copyInputStreamToFile(file.getInputStream(),
////                    new File("/usr/local/software/static", System.currentTimeMillis() + extName));
//            //构造一个带指定Zone对象的配置类
//            Configuration cfg = new Configuration(Zone.zone0());
//            //...其他参数参考类注释
//            UploadManager uploadManager = new UploadManager(cfg);
//            //...生成上传凭证，然后准备上传
//            String accessKey = "980GXeQWtBnCYarm0U48F8_75Fy5vbUqHsUYH6In";
//            String secretKey = "pennMD4rVB7FuUqaU0auWkOous_GUhW7NR17zv1_";
//            String bucket = "laomeng_test";
//            //默认不指定key的情况下，以文件内容的hash值作为文件名
//            String key = null;
//            try {
//                ByteArrayInputStream byteInputStream=new ByteArrayInputStream(file.getBytes());
//                Auth auth = Auth.create(accessKey, secretKey);
//                String upToken = auth.uploadToken(bucket);
//                try {
//                    Response response = uploadManager.put(byteInputStream,key,upToken,null, null);
//                    //解析上传成功的结果
//                    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//                    System.out.println(putRet.key);
//                    System.out.println(putRet.hash);
//                } catch (QiniuException ex) {
//                    Response r = ex.response;
//                    System.err.println(r.toString());
//                    try {
//                        System.err.println(r.bodyString());
//                    } catch (QiniuException ex2) {
//                        //ignore
//                    }
//                }
//            } catch (UnsupportedEncodingException ex) {
//                //ignore
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        //上传成功返回原来页面
//        return "/upload";
//    }

}
